package com.xh.shuati.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.common.utils.AddressUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * canal配置
 */
@Component
@Data
@Configuration
@Slf4j
@ConfigurationProperties("canal")
public class CanalConfig {

    /**
     * 端口
     */
    private Integer port;
    /**
     * 描述
     */
    private String destination;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 批量大小
     */
    private Integer batchSize;

    /**
     * 过滤
     */
    private String filter;

    /**
     * 实体类包名前缀
     */
    private String basePackage;

    @Bean(destroyMethod = "disconnect")
    public CanalConnector canalConnector() {
        log.info("Canal配置初始化");
        return CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),
                this.port), this.destination, this.username, this.password);
    }
}
